<?php
$themename="lfdh";   

define('LFDH_LIB', str_replace("\\",'/',TEMPLATEPATH) . '/lib');
define('LFDH_FUNCTIONS', LFDH_LIB . '/functions');
define('LFDH_WIDGETS', LFDH_LIB . '/functions/widgets');

define('LFDH_THEME_FOLDER',str_replace("\\",'/',TEMPLATEPATH));
define('LFDH_THEME_PATH','/' . substr(LFDH_THEME_FOLDER,stripos(LFDH_THEME_FOLDER,'wp-content')));
define('LFDH_THEME_PATH_LIB', LFDH_THEME_PATH . '/lib');
define('LFDH_THEME_PATH_FUNCTIONS', LFDH_THEME_PATH_LIB . '/functions');
define('LFDH_THEME_PATH_WIDGETS', LFDH_THEME_PATH_LIB . '/functions/widgets');

    // turn off update wordpress
    add_filter( 'pre_site_transient_update_core', create_function( '$a', "return null;" ) );
    remove_action( 'load-update-core.php', 'wp_update_themes' );
    add_filter( 'pre_site_transient_update_themes', create_function( '$a', "return null;" ) );
    
    // Load jQuery
	if ( !is_admin() ) {
	   wp_deregister_script('jquery');
	   wp_register_script('jquery', get_bloginfo('template_directory')."/js/jquery.min.js");
	   wp_enqueue_script('jquery');
	}
    
    // Clean up the <head>
	function removeHeadLinks() {
    	remove_action('wp_head', 'rsd_link');
    	remove_action('wp_head', 'wlwmanifest_link');
    }
    add_action('init', 'removeHeadLinks');
    remove_action('wp_head', 'wp_generator');
    
    //  menu 
	if ( function_exists( 'add_theme_support' ) ) { 
	add_theme_support('menus');
    }
	if (function_exists('register_nav_menus')) {
		register_nav_menus(
			array(
				'menu_header' => 'Main menu header',
                'menu_footer' => 'Menu footer'
			)
		);
	}
    
    // support thumbnails 
	if ( function_exists( 'add_theme_support' ) ) { 
	   add_theme_support( 'post-thumbnails' );
       add_image_size('featured_preview', 55, 55, true); 
    }
    
    //install plugin : Multiple Post Thumbnails
    // and add this code:
    if (class_exists('MultiPostThumbnails')) {
	            new MultiPostThumbnails(array(
	            'label' => 'Second Featured Image',
	            'id' => 'second-featured-image',
	            'post_type' => 'post'
	            )
	        );
	}
    
    
    // support excerpt 
   	if ( function_exists( 'add_post_type_support' ) ) { 
	   add_post_type_support('page', 'excerpt');
    }

    // Limit Excerpt
    function excerpt($limit) {
      $excerpt = explode(' ', get_the_excerpt(), $limit);
      if (count($excerpt)>=$limit) {
        array_pop($excerpt);
        $excerpt = implode(" ",$excerpt).'';
      } else {
        $excerpt = implode(" ",$excerpt);
      }	
      $excerpt = preg_replace('`\[[^\]]*\]`','',$excerpt);
      return $excerpt;
    }
    // Limit Content  
    function content($limit) {
      $content = explode(' ', get_the_content(), $limit);
      if (count($content)>=$limit) {
        array_pop($content);
        $content = implode(" ",$content).'';
      } else {
        $content = implode(" ",$content);
      }	
      $content = preg_replace('/\[.+\]/','', $content);
      $content = apply_filters('the_content', $content); 
      $content = str_replace(']]>', ']]&gt;', $content);
      return $content;
    }
    // Limit text
	 function textlimit( $textcontent , $limit) {
	  $content = explode(' ', $textcontent, $limit);
	  if (count($content)>=$limit) {
		array_pop($content);
		$content = implode(" ",$content).'';
	  } else {
		$content = implode(" ",$content);
	  }	
	  return $content;
	}
    
    //	Remove Items from the Post and Page Columns
    function custom_post_columns($defaults) {
      unset($defaults['comments']);
      unset($defaults['tags']);
      return $defaults;
    }
    add_filter('manage_posts_columns', 'custom_post_columns');  
    function custom_pages_columns($defaults) {
      unset($defaults['comments']);
      return $defaults;
    }
    add_filter('manage_pages_columns', 'custom_pages_columns');
    function custom_media_columns($defaults) {
      unset($defaults['comments']);
      return $defaults;
    }
    add_filter('manage_media_columns', 'custom_media_columns');

    // remove submenu items under the top-level navigation
    function remove_submenus() {
      global $submenu;
      unset($submenu['index.php'][10]); // Removes 'Updates'.
      unset($submenu['edit.php'][16]); // Removes 'Tags'.  
    }
    add_action('admin_menu', 'remove_submenus');
        
    //Removing Menu Items
    function remove_menu_items() {
      global $menu;
      $restricted = array( __('Links'), __('Comments'));
      end ($menu);
      while (prev($menu)){
        $value = explode(' ',$menu[key($menu)][0]);
        if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){
          unset($menu[key($menu)]);}
        }
      }
    add_action('admin_menu', 'remove_menu_items');   
      
    //	Disable Dashboard Widgets
    function remove_dashboard_widgets(){
      global$wp_meta_boxes;
      unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
      unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);
      unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
      unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);
     // unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
      unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']); 
      unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_quick_press']);
      unset($wp_meta_boxes['dashboard']['normal']['high']['dashboard_browser_nag']);
      unset($wp_meta_boxes['dashboard']['normal']['core']['wp_welcome_panel']);
    }
    add_action('wp_dashboard_setup', 'remove_dashboard_widgets');
    
    
    //Remove WordPress Version Number
    function wpbeginner_remove_version() {
	   return '';
	}
	add_filter('the_generator', 'wpbeginner_remove_version');
    remove_action('wp_head', 'wp_generator');
        
    function remove_footer_admin () {
    	echo '';
	} 
	add_filter('admin_footer_text', 'remove_footer_admin');
        
    // remove wp widgets
    function remove_some_wp_widgets(){
      unregister_widget('WP_Widget_Calendar');
      unregister_widget('WP_Widget_Search');
      unregister_widget('WP_Widget_Recent_Comments');
      unregister_widget('WP_Widget_Archives');
      unregister_widget('WP_Widget_Links');
      unregister_widget('WP_Widget_Meta');
      unregister_widget('WP_Widget_Categories');
      unregister_widget('WP_Widget_Tag_Cloud');
      unregister_widget('WP_Widget_Pages');
      unregister_widget('WP_Widget_RSS');
      unregister_widget('WP_Nav_Menu_Widget');
      unregister_widget('WP_Widget_Recent_Posts');
      unregister_widget('WP_Widget_Text');
    }
    add_action('widgets_init','remove_some_wp_widgets', 1);
    
    //remove editor menu
    function remove_editor_menu() {
      remove_action('admin_menu', '_add_themes_utility_last', 101);
    }
    add_action('_admin_menu', 'remove_editor_menu', 1);        
    
    // turn off wp_welcome_panel
    add_action( 'load-index.php', 'hide_welcome_panel' );
    function hide_welcome_panel() {
        $user_id = get_current_user_id();
    
        if ( 1 == get_user_meta( $user_id, 'show_welcome_panel', true ) )
            update_user_meta( $user_id, 'show_welcome_panel', 0 );
    }
    
    function is_telnum($telnum)
    {
        // $regexp = '/^[0-9\+\-]{7,}$/';
        $regexp = '/^[0-9\+\ \-\(\)]{1,}$/';
        if(preg_match($regexp, $telnum))
            return true;
        else
            return false;
    }
    
    //Removing Meta Boxes from Editor Screen
    function remove_extra_meta_boxes() {
        remove_meta_box( 'postcustom' , 'post' , 'normal' ); // custom fields for posts
        remove_meta_box( 'postcustom' , 'page' , 'normal' ); // custom fields for pages
     //   remove_meta_box( 'postexcerpt' , 'post' , 'normal' ); // post excerpts
      //  remove_meta_box( 'postexcerpt' , 'page' , 'normal' ); // page excerpts
        remove_meta_box( 'commentsdiv' , 'post' , 'normal' ); // recent comments for posts
        remove_meta_box( 'commentsdiv' , 'page' , 'normal' ); // recent comments for pages
        remove_meta_box( 'tagsdiv-post_tag' , 'post' , 'side' ); // post tags
        remove_meta_box( 'tagsdiv-post_tag' , 'page' , 'side' ); // page tags
        remove_meta_box( 'trackbacksdiv' , 'post' , 'normal' ); // post trackbacks
        remove_meta_box( 'trackbacksdiv' , 'page' , 'normal' ); // page trackbacks
        remove_meta_box( 'commentstatusdiv' , 'post' , 'normal' ); // allow comments for posts
        remove_meta_box( 'commentstatusdiv' , 'page' , 'normal' ); // allow comments for pages
        remove_meta_box('slugdiv','post','normal'); // post slug
        remove_meta_box('slugdiv','page','normal'); // page slug
     //   remove_meta_box('pageparentdiv','page','side'); // Page Parent
     
     // add postty support
        add_post_type_support( 'post', 'page-attributes' );
        }
    add_action( 'admin_menu' , 'remove_extra_meta_boxes' );   
        
        
    // Newsletter Ajax 1
    // echo emailnews_plugin_url('widget/widget.js');    
    wp_enqueue_script( 'ajax-newsletter', emailnews_plugin_url('widget/widget.js'), array('jquery')); // jQuery will be included automatically

    //echo get_bloginfo('template_directory').'/lib/js/ajax.js';
    wp_localize_script( 'ajax-newsletter', 'ajax_object_newsletter', array( 'ajaxurl_newsletter' => admin_url( 'admin-ajax.php' ) ) ); // setting ajaxurl
    
    add_action( 'wp_ajax_ajax_newsletter', 'ajax_action_newsletter' ); // ajax for logged in users
    add_action( 'wp_ajax_nopriv_ajax_newsletter', 'ajax_action_newsletter' ); // ajax for not logged in users
    function ajax_action_newsletter() {

        $Email = $_POST['Email'];
        
        global $wpdb, $wp_version;
        define("WP_eemail_TABLE_SUB", $wpdb->prefix . "eemail_newsletter_sub");
        $cSql = "select * from ".WP_eemail_TABLE_SUB." where eemail_email_sub='" . mysql_real_escape_string(trim($Email)). "'";
        $data = $wpdb->get_results($cSql);
        if ( empty($data) ) 
        {
        	$sql = "insert into ".WP_eemail_TABLE_SUB.""
        		. " set `eemail_name_sub` = 'NONAME"
        		. "', `eemail_email_sub` = '" . mysql_real_escape_string(trim($Email))
        		. "', `eemail_status_sub` = 'YES"
        		. "', `eemail_date_sub` = CURDATE()";
        	
        	$wpdb->get_results($sql);
            $result['result'] = "succ"; 
        	echo "succ";//json_encode($result);
        }
        else
        {
        	$result['result'] = "exs"; 
        	echo "exs";//json_encode($result);
        }
    	exit;
    } 
    // End  Newsletter Ajax 1
    
    // Newsletter Ajax 2
    // echo emailnews_plugin_url('widget/widget.js');    
    wp_localize_script( 'ajax-newsletter', 'ajax_object_newsletter', array( 'ajaxurl_newsletter' => admin_url( 'admin-ajax.php' ) ) ); // setting ajaxurl
    add_action( 'wp_ajax_ajax_newsletter2', 'ajax_action_newsletter2' ); // ajax for logged in users
    add_action( 'wp_ajax_nopriv_ajax_newsletter2','ajax_action_newsletter2' ); // ajax for not logged in users
    function ajax_action_newsletter2() {

        $Email = $_POST['Email'];
        
        global $wpdb, $wp_version;
        define("WP_eemail_TABLE_SUB", $wpdb->prefix . "eemail_newsletter_sub");
        $cSql = "select * from ".WP_eemail_TABLE_SUB." where eemail_email_sub='" . mysql_real_escape_string(trim($Email)). "'";
        $data = $wpdb->get_results($cSql);
        if ( ! empty($data) ){
            foreach ( $data as $data ) {
                 $DID = $data->eemail_id_sub;    
            }
        }
        if ( empty($data) ) 
        {
        	$result['result'] = "exs"; 
        	echo "exs";//json_encode($result);
        }
        else
        {
            $wpdb->get_results("delete from ".WP_eemail_TABLE_SUB." where eemail_id_sub=".$DID);
            $result['result'] = "succ"; 
        	echo "succ";//json_encode($result);
        }
    	exit;
    } 
    // End  Newsletter Ajax 2

    // for custom meta
    function media_meta_clean(&$arr)
    {
    	if (is_array($arr))
    	{
    		foreach ($arr as $i => $v)
    		{
    			if (is_array($arr[$i])) 
    			{
    				media_meta_clean($arr[$i]);
     
    				if (!count($arr[$i])) 
    				{
    					unset($arr[$i]);
    				}
    			}
    			else 
    			{
    				if (trim($arr[$i]) == '') 
    				{
    					unset($arr[$i]);
    				}
    			}
    		}
     
    		if (!count($arr)) 
    		{
    			$arr = NULL;
    		}
    	}
    }
    
    require_once(LFDH_FUNCTIONS . '/custom_meta/page-meta-media-category.php');
    require_once(LFDH_FUNCTIONS . '/custom_meta/page-meta-bigandsmalltext.php');
    require_once(LFDH_FUNCTIONS . '/custom_meta/page-meta-ref.php');
    require_once(LFDH_WIDGETS . '/facebook_link_widget.php');
    require_once(LFDH_WIDGETS . '/show_posts_categories.php');
    require_once(LFDH_WIDGETS . '/solutions_text2_widget.php');
    require_once(LFDH_WIDGETS . '/widgets.php');
    
    
    
    //Adding the Open Graph in the Language Attributes
	function add_opengraph_doctype( $output ) {
	        return $output . '  xmlns:fb="http://ogp.me/ns/fb#" ';
	    }
	add_filter('language_attributes', 'add_opengraph_doctype');
	//Lets add Open Graph Meta Info

	function insert_fb_in_head() {
	    global $post;
	    if ( (!is_singular())&&(!is_page())) return;
        
        echo '<meta property="og:title" content="' . get_the_title() . '"/>';
        echo '<meta property="og:type" content="article"/>';
        
        if(!has_post_thumbnail( $post->ID )) {
            echo '<meta property="og:image" content="' . get_bloginfo('template_url') . '/images/lfdh_logo_facebook.jpg"/>';
        }else{
            $thumbnail_src = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'medium' );
            echo '<meta property="og:image" content="' . esc_attr( $thumbnail_src[0] ) . '"/>';
        }
        echo '<meta property="og:url" content="' . get_permalink() . '"/>';
        echo '<meta property="og:site_name" content="'.get_bloginfo('name').'"/>';
        echo '<meta property="fb:admins" content="138702626244959"/>'; 
	}
	add_action( 'wp_head', 'insert_fb_in_head', 6 );
    
    // change login page logo
    add_action("login_head", "lfdh_login_head");
    function lfdh_login_head() {
    	echo "
    	<style>
    	body.login #login h1 a {
    		background: url('".get_bloginfo('template_url')."/images/logo-login.png') no-repeat scroll center top transparent;
    	}
    	</style>
    	";
    }
    
    add_action("admin-bar", "lfdh_admin_bar");
    function lfdh_admin_bar() {
    	echo "
    	<style>
        	#wpadminbar .csp-fr_FR b{
        		margin-left:20px !importion;
        	}
    	</style>
    	";
    }
    
    // add more custom column
    add_filter('manage_posts_columns', 'post_menu_order_columns');
    function post_menu_order_columns($defaults) {
        $defaults['post_menu_order'] = __('Order');
        return $defaults;
    }
    add_action('manage_posts_custom_column', 'post_menu_order_custom_column', 10, 2);
    function post_menu_order_custom_column($column_name, $post_id) {
        global $post;
        if ($column_name == 'post_menu_order') {  
                $order = $post->menu_order;
                echo $order; 
        }  
    }

// Add a Custom Column in Posts and Custom Post Types Admin Screen
// show featured_image
    // GET FEATURED IMAGE
    function ST4_get_featured_image($post_ID) {
        $post_thumbnail_id = get_post_thumbnail_id($post_ID); 
        if ($post_thumbnail_id) {
            $post_thumbnail_img = wp_get_attachment_image_src($post_thumbnail_id, 'featured_preview');
            return $post_thumbnail_img[0];
        }
    }
        // ADD NEW COLUMN  
    function ST4_columns_head($defaults) {  
        $defaults['featured_image'] = __('Featured Image');
        return $defaults;  
    }
     
    // SHOW THE FEATURED IMAGE  
    function ST4_columns_content($column_name, $post_ID) {  
        if ($column_name == 'featured_image') {  
            $post_featured_image = ST4_get_featured_image($post_ID);  
            if ($post_featured_image) {  
                echo '<img width="70" height="50" src="' . $post_featured_image . '" />';
            }  
        }  
    }  
    add_filter('manage_posts_columns', 'ST4_columns_head');  
    add_action('manage_posts_custom_column', 'ST4_columns_content', 10, 2);  
    


?>